package com.idc.resource.util;

import java.util.Random;

/**
 * 随机码生成
 * Created by Administrator on 2017/7/15 0015.
 */
public class RandomCodeUtil {

    /**
     * 生成带字母的随机数
     * @param length 随机数长度
     * @return 随机数
     */
    public static String buildCode(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 生成带纯数字的随机数
     * @param length 随机数长度
     * @return 随机数
     */
    public static String buildNumCode(int length) {
        String val = "";
        Random random = new Random();
        for (int i=0;i<length;i++){
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }
}
